package com.ruoyi.ratedemo.rate;

import com.ruoyi.ratedemo.domain.Rate;
import com.ruoyi.ratedemo.mapper.RateMapper;
import com.ruoyi.ratedemo.service.RateService;
import com.ruoyi.ratedemo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rate")
public class RateController {
    @Autowired
    private RateService rateService;
    @Autowired
    private RateMapper rateMapper;
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
    @PostMapping("/add")
    public Result addRate(@RequestBody Rate rate){
        int bookId = rate.getBookId();
        int rate1 = rate.getRate();
        int userId =rate.getUserId();
        rateMapper.AddRate(rate1, bookId, userId);
        return Result.success();
    }
    @GetMapping("/get/{book_id}")
    public Result getRate(@PathVariable int book_id){
        double avg_r = rateMapper.getRate(book_id);
        return Result.success(avg_r);
    }
    @GetMapping("/list")
    public Result list(){
        return Result.success(rateMapper.rateList());
    }
}
